﻿using Caltech.Models;
using CaltechSoft.Service.CommandService;
using CaltechSoft.Service.ReportService.Standard;
using CaltechSoft.Service.SearchCriteria;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Caltech.Results;
using ServiceModel = CaltechSoft.Service.DBCommand;
using AutoMapper;
using Caltech.Extensions;
using Caltech.Service.Common;

namespace Caltech.Controllers
{
    [HasAccess(Areas.Standard)]
    public class StandardController : ApiController
    {

        private readonly ICertificateStandardCommandService _standardCommandService;
        private readonly IStandardReportService _standardReportService;

        public StandardController(ICertificateStandardCommandService standardCommandService, IStandardReportService standardReportService
            )
        {
            _standardCommandService = standardCommandService;
            _standardReportService = standardReportService;
        }

        // GET api/<controller>
        public IHttpActionResult Get(string standardNo = "")
        {
            var pageSize = Convert.ToInt32(Request.GetQueryNameValuePairs().FirstOrDefault(n => n.Key == "count").Value);
            var pageIndex = Convert.ToInt32(Request.GetQueryNameValuePairs().FirstOrDefault(n => n.Key == "page").Value) - 1;

            var searchCriteria = new StandardSearchCriteria();
            searchCriteria.Keyword = standardNo;

            var result = _standardReportService.Search(searchCriteria, pageSize, pageIndex);

            return Request.CreateSearchResult<StandardModel>(result.Items.Select(x => new StandardModel()
                {
                    CertificateStandardId = x.CertificateStandardId,
                    StandardNo = x.StandardNo,
                    ModelNumber = x.ModelNumber,
                    Manufacturer = x.Manufacturer,
                    DueDate = x.DueDate,
                    TestReportNumber = x.TestReportNumber,
                    TechnicianName = x.TechnicianName,
                    Description = x.Description
                }), result.Count);
        }

        // GET api/<controller>/5
        public IHttpActionResult Get(int id)
        {
            var result = _standardReportService.SearchById(id);
            return Ok(result);
        }

        public int Post(StandardModel model)
        {
            model.CreatedDate = DateTime.Now;
            var createModel = Mapper.Map<StandardModel, ServiceModel.CreateCertificateStandardCommand>(model);
            return _standardCommandService.AddStandard(createModel);
        }

        // PUT api/<controller>/5
        public void Put(int id, StandardModel model)
        {
            model.UpdatedDate = DateTime.Now;
            var createModel = Mapper.Map<StandardModel, ServiceModel.CreateCertificateStandardCommand>(model);
            _standardCommandService.UpdateCertificateStandard(createModel);
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            _standardCommandService.Delete(id);
        }
    }
}
